
!function( $ ) {
	//定义构造函数
	var RegionTree = function (ele,opt){
		this.$element = $(ele);
		this.defaults = {
				name:'treeDemo',
				url:'../system/sysRegionVO/querySysRegionDataTree',
				level: "3",
				isShow:false,
				checkType:"checkbox",
				parentId:"1",
				minLevel:"3",
				callBack: null
		},
		this.options= $.extend({},this.defaults,opt);
		
	};
	//定义方法
	RegionTree.prototype= {
		init:function (options){
//			console.log("cityTree:init.")
			var zTreeObj;
			var name = this.options.name;
			var callBack = this.options.callBack;
			var url = this.options.url;
			var level = this.options.level;
			var checkType = this.options.checkType;
			var parentId = this.options.parentId;
			var minLevel = this.options.minLevel;
			
			var html = "<div class='row-fluid'><!-- 帮助基本信息详情 -->" +
					"<div id='"+name+"CityTreeDlg' class='modal hide fade' backdrop=false tabindex='-1' data-width='300'>" +
						"<div class='modal-dialog'>" +
							"<div class='modal-content'>" +
								"<div class='modal-header bg-yellow'>" +
									"<button type='button' class='close' data-dismiss='modal' aria-hidden='true'></button>" +
									"<h4 class='modal-title'>" +
										"<i class='fa fa-plus'></i>" +
										"<span id='lblAddTitle' style='font-weight:bold'>行政区</span>" +
									"</h4>" +
								"</div>	" +
							"<div class='modal-body tabbable tabbable-custom '>" +
								"<ul id='"+name+"' class='ztree'></ul>" +
								"<div class='modal-footer' >" +
									"<button type='button' data-dismiss='modal' class='btn btn-info btn-outline m-wrap' id='"+name+"ZTreeSaveBtn'>确定</button>"+
									"<button type='button' data-dismiss='modal' class='btn btn-auto btn-outline'>关闭</button>" +
								"</div>" +
							"</div>" +
						"</div>" +
					"</div>" +
				"</div>" +
			"</div>";
			$(".container-fluid").prepend(html);
//			console.log("cityTree:appendDlg.")
			//查询数据后回调函数
			function doDraw(data,ele){
				var setting = {
					check: {
						enable: true,
						chkboxType: { "Y" : "ps", "N" : "ps" }
					},
					data: {
						simpleData: {
							enable: true
						}
					}
				};
				//控制单选多选
				if(checkType=="radio"){
					setting.check.chkStyle = "radio";
					setting.check.radioType = "all";
				}
				var zNodes = [];
				var list = data;
				
				var idList={};
				if(parentId != "1"){
					//获取当前父节点下所有的子节点
					function getDownID(id,level){
						idList[id+""]=true;
						for(var i=0; i<list.length;i++){
							var city = list[i];
							var dRegionId = city.regionId;
							var dParentId = city.parentId;
							var dRegionLevel = city.regionLevel;
							if(dRegionLevel<level){
								if((dRegionId == (id+""))||(dParentId == id)){
									getDownID(dRegionId,"dRegionLevel");
								}
							}
						}
					}
					getDownID(parentId,minLevel);
				}
				
				for(var i=0; i<list.length;i++){
					var city = list[i];
					var dRegionId = city.regionId;
					var dParentId = city.parentId;
					var dRegionName = city.regionName;
					var dRegionLevel = city.regionLevel;
					var isAdd = false;
					if(parentId == "1"){
						isAdd = true;
					}else{
						if(idList[dRegionId]||idList[dParentId+""]){
							isAdd = true;
						}
					}
					if(isAdd){
						var tempData = {};
						tempData.id = dRegionId;
						tempData.pId = dParentId;
						tempData.name = dRegionName;
						if(city.regionId == parentId){
							tempData.open = true;
						}else{
							tempData.open = false;
						}
						zNodes.push(tempData);
					}
				}
			    zTreeObj = $.fn.zTree.init($("#"+name), setting, zNodes);
			    //确定按钮回调函数
			    $("#"+name+'ZTreeSaveBtn').bind('click',{ele:ele,callBack:callBack,name:name},function(e){
			    	var treeName = e.data.name;
			    	var treeObj = $.fn.zTree.getZTreeObj(treeName);
					var nodes = treeObj.getCheckedNodes(true);
					var ids = "";
					var names = "";
					var j=0;
		            for(var i=0;i<nodes.length;i++){
		            	ids+=nodes[i].id;
		            	names+=nodes[i].name;
		            	if(i<nodes.length-1){
		            		ids+=",";
		            		names+=",";
		            	}
		            	j++;
		            }
		            //设置值
		            e.data.ele.attr("value",ids);
		            //修改数目
		            e.data.ele.children('.badge-important').text(j); 
		            if(typeof callBack == "function"){
		            	callBack(ids);
		            }
				});
			}
			//全部
			//../system/sysRegionVO/querySysRegionDataTree?regionLevel=4
			//区县:regionLevel=3
			//市:regionLevel=2
			//省:regionLevel=1
//			$.post("../system/sysRegionVO/querySysRegionDataTree?regionLevel=1",function(data){
			var ele = this.$element;
			$.ajax({  
				type : "post",  
		        url : url+"?regionLevel="+level,  
		        async : true,  
		        success : function(data){  
		        	doDraw(data,ele);
		        }  
		    }); 
			this.$element.click(function(){
				var $this = $(this);
				var data = $this.data('regionTree');
				data.show();
			})
		},
		
		show:function(e){
//			console.log("cityTree:show.")
			var name = this.options.name;
			//进行数据回显
			var ele = this.$element;
			var val = ele.attr("value");
			function filter(node) {
				return node;
			}
			if(val){
				var treeObj = $.fn.zTree.getZTreeObj(name);
				var allNodes = treeObj.getNodes();
				var nodes = treeObj.getNodesByFilter(filter);
				for (var i = 0; i < nodes.length; i++) {
					var node = nodes[i];
					treeObj.checkNode(node, false, false);
					treeObj.updateNode(node);
				}
				var list =val.split(",");
				for(var i=0;i<list.length;i++){
					var node = treeObj.getNodeByParam("id",list[i] );
//					console.log(node);
					node.checked = true;
					treeObj.checkNode(node, true, false);
					treeObj.updateNode(node);
				}
			}
			$("#"+name+"CityTreeDlg").modal("show");
			if (e) {
				e.preventDefault();
			}
		}
	};
	$.fn.regionTree = function(options,ele) {
		 var args = Array.apply(null, arguments);
		 args.shift();
		 if(this.selector!=""){
			 
		 }
		 this.each(function () {
			var $this = $(this),
				data = $this.data('regionTree');
			if (!data) {
				data = new RegionTree(this, options);
				$this.data('regionTree', data);
				data.init("options")
			}else{
				
			}
			if (typeof options == 'string' && typeof data[options] == 'function') {
				internal_return = data[options].apply(data, args);
				return false;
			}
		});
	 }
	 
}( window.jQuery );